package com.ycp.erp.role.ui.binder;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.one.common.view.multitytype.MultiTypeAdapter;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.common.view.widget.ScrollLinearLayoutManager;
import com.ycp.erp.R;
import com.ycp.erp.role.model.bean.Permissions;
import com.ycp.erp.role.model.bean.Role;
import com.ycp.erp.role.ui.view.SelectInter;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * Class Des:
 * Created by bjh on 2021/9/14.
 */
public class PermissionFBinder extends BaseItemBinder<Permissions> {

    @BindView(R.id.ivSelect)
    ImageView ivSelect;
    @BindView(R.id.tvItem)
    TextView tvItem;
    @BindView(R.id.ivArrowDown)
    ImageView ivArrowDown;
    @BindView(R.id.rlItem)
    RelativeLayout rlItem;
    @BindView(R.id.rcv)
    RecyclerView rcv;
    private MultiTypeAdapter adapter;

    public PermissionFBinder() {
        super(R.layout.item_permission);
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull Permissions item) {
        ImageView ivSelect = holder.getView(R.id.ivSelect);
        TextView tvItem = holder.getView(R.id.tvItem);
        ImageView ivArrowDown = holder.getView(R.id.ivArrowDown);
        tvItem.setText(item.getFirstName());
        RelativeLayout rlItem = holder.getView(R.id.rlItem);
        RecyclerView rcv = holder.getView(R.id.rcv);
        ScrollLinearLayoutManager scrollLinearLayoutManager = new ScrollLinearLayoutManager(mContext);
        scrollLinearLayoutManager.setScrollEnable(false);
        rcv.setLayoutManager(scrollLinearLayoutManager);
        adapter = new MultiTypeAdapter();
        PermissionSBinder  binder = new PermissionSBinder(new SelectInter() {
            @Override
            public void isSelect() {
                item.setSelect(true);
                ivSelect.setImageResource(R.mipmap.ic_selected_circle);
            }
        });
        adapter.register(Permissions.PermissionsS.class, binder);
        adapter.setItems(item.getPermissionsS());
        rcv.setAdapter(adapter);
        if (item.isSelect()) {
            ivSelect.setImageResource(R.mipmap.ic_selected_circle);
        } else {
            ivSelect.setImageResource(R.mipmap.ic_unselected_circle);
        }
        ivSelect.setOnClickListener(v -> {
            item.setSelect(!item.isSelect());
            if (item.isSelect()) {
                ivSelect.setImageResource(R.mipmap.ic_selected_circle);
            } else {
                ivSelect.setImageResource(R.mipmap.ic_unselected_circle);
            }
        });
        rlItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if (item.getPermissionsS()!=null&&item.getPermissionsS().size()>0){
                   if (!rlItem.isSelected()){
                       rcv.setVisibility(View.VISIBLE);
                   }else {
                       rcv.setVisibility(View.GONE);
                   }
                   rlItem.setSelected(!rlItem.isSelected());
               }
            }
        });
        if(item.getPermissionsS()!=null&&item.getPermissionsS().size()>0){
            ivArrowDown.setVisibility(View.VISIBLE);
        }
    }
}
